package cn.wzy.service.impl;

import cn.wzy.enums.BaseConstants;
import cn.wzy.mapper.CarTypeMapper;
import cn.wzy.pojo.CarType;
import cn.wzy.results.ResponseResult;
import cn.wzy.service.CarTypeService;
import cn.wzy.utils.RedisCache;
import cn.wzy.vo.GoPageVo;
import cn.wzy.vo.PageVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * (CarType)表服务实现类
 *
 * @author makejava
 * @since 2023-01-24 10:49:10
 */
@Service
public class CarTypeServiceImpl extends ServiceImpl<CarTypeMapper, CarType> implements CarTypeService {

    @Resource
    private RedisCache redisCache;

 public ResponseResult getCarTypePage(GoPageVo goPageVo) {
        Page<CarType> page = new Page<>(goPageVo.getPageIndex(),goPageVo.getPageSize());
        LambdaQueryWrapper<CarType> queryWrapper = new LambdaQueryWrapper<>();
        if (goPageVo.getKeyword()!=null && !"".equals(goPageVo.getKeyword())){
            queryWrapper.like(CarType::getName,goPageVo.getKeyword());
        }
        List<CarType> productTypes = page(page, queryWrapper).getRecords();
        PageVo pageVo = new PageVo(productTypes, page.getTotal(), goPageVo.getPageSize());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult saveCarType(CarType carType) {
        //        判断是否id存在，操作类型
        if (Objects.isNull(carType.getId())){
//            添加操作
            save(carType);
            redisCache.deleteObject(BaseConstants.Redis.CAR_TYPE_TREE);
            return ResponseResult.okResult();
        }else {
//            修改操作
            updateById(carType);
            redisCache.deleteObject(BaseConstants.Redis.CAR_TYPE_TREE);
            return ResponseResult.okResult();
        }
    }

    @Override
    public void deleteCarType(Long id) {
        delete(id);
        redisCache.deleteObject(BaseConstants.Redis.CAR_TYPE_TREE);
    }

    private void delete(Long id) {
        LambdaQueryWrapper<CarType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CarType::getPid, id);
        List<CarType> list = list(queryWrapper);
        if (list == null || list.size() <1){
            removeById(id);
            return;
        }
        for (CarType carType : list) {
            deleteCarType(carType.getId());
            removeById(id);
        }
        return;
    }

    @Override
    public ResponseResult deleteCarTypeList(List<Long> ids) {
       removeByIds(ids);
       return ResponseResult.okResult();
    }


    /*
    * 无限极树
    * */
    @Override
    public List<CarType> tree(Long id) {
//        方案一：递归
//        LambdaQueryWrapper<CarType> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(CarType::getPid,id);
//        List<CarType> children = list(queryWrapper);
//        if (children==null){
//            return null;
//        }
//        for (CarType child : children) {
//            List<CarType> grandchildren = tree(child.getId());
//            child.setChildren(grandchildren);
//        }
//        return children;
        Object cacheObject = carTypeCache();
        if (Objects.nonNull(cacheObject)){
            return (List<CarType>) cacheObject;
        }
        synchronized (CarTypeServiceImpl.class){
            Object redisCacheCacheObject = carTypeCache();
            if (Objects.nonNull(redisCacheCacheObject)){
                return (List<CarType>) redisCacheCacheObject;
            }
            List<CarType> carTypeList = treeLoop(id);
            if (Objects.isNull(carTypeList)){
                redisCache.setCacheObject(BaseConstants.Redis.CAR_TYPE_TREE,new ArrayList<>());
                redisCache.setCacheObject(BaseConstants.Redis.CAR_TYPE_TREE_BAS,new ArrayList<>());
            }else {
                redisCache.setCacheObject(BaseConstants.Redis.CAR_TYPE_TREE,carTypeList);
                redisCache.setCacheObject(BaseConstants.Redis.CAR_TYPE_TREE_BAS,carTypeList);
            }
            return carTypeList;
        }
    }
    public List<CarType> carTypeCache(){
        Object cacheObject = redisCache.getCacheObject(BaseConstants.Redis.CAR_TYPE_TREE);
        if (Objects.nonNull(cacheObject)){
            return (List<CarType>) cacheObject;
        }
        Object cacheObject1 = redisCache.getCacheObject(BaseConstants.Redis.CAR_TYPE_TREE_BAS);
        if (Objects.nonNull(cacheObject1)){
            return (List<CarType>) cacheObject1;
        }
        return null;
    }
//    方案二：循环
    public List<CarType> treeLoop(Long id){
        List<CarType> allCarTypes = list();
        HashMap<Long, CarType> map = new HashMap<>();
        List<CarType> carTypeList = new ArrayList<>();
        for (CarType allCarType : allCarTypes) {
            map.put(allCarType.getId(),allCarType);
        }
        for (CarType allCarType : allCarTypes) {
            if (allCarType.getPid() == id){
                carTypeList.add(allCarType);
            }else {
                CarType carType = map.get(allCarType.getPid());
                carType.getChildren().add(allCarType);
            }
        }
        return carTypeList;
    }

    @Override
    public ResponseResult getCarType(Long id) {
       CarType carType = getById(id);
       return ResponseResult.okResult(carType);
    }
}

